<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\facade\Log;

/**
 * 流量情况接口
 */
class Flow extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = [];
    /**
     * 默认响应输出类型,支持json/xml
     * @var string 
     */
    // protected $responseType = '';

    public function data()
    {
        //处理参数
        $type = $this->request->post('type');
        if(!$type){
            $this->error(__('Missed parameters'));
        }
        if(!in_array($type, config('flow')['type'])){
            $this->error(__('Invalid parameters'));
        }
        switch ($type) {
            case 'flow':
                $input_arr = array(
                    "type"=>'flowchart_10', 
                    "bridge"=>0
                );
                break;
            case 'link':
                $input_arr = array(
                    "type"=>'flowchart_flowcnt', 
                    "bridge"=>0
                );
                break;
            case 'trenddown':
                $input_arr = array(
                    "type"=>'appgroupchart', 
                    "charttype"=>'bpsin',
                    "bridge"=>0,
                    "hours"=>24,
                    "tmend"=>0,
                    "group"=>'',
                );
                break;
            case 'trendup':
                $input_arr = array(
                    "type"=>'appgroupchart',
                    "charttype"=>'bpsout',
                    "bridge"=>0,
                    "hours"=>24,
                    "tmend"=>0,
                    "group"=>'',
                );
                break;
            case 'snap':
                $input_arr = array(
                    "type"=>'datasnap',
                    "bridge"=>0
                );
                break;
            default:
                # code...
                break;
        }
        $data_string = http_build_query($input_arr);
        $cookie_jar = $this->getPanabitCookieJar();
        //发请求拿数据
        $curlopt = array(
            // CURLOPT_HTTPHEADER=>array ('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen ( $data_string ) ),
            CURLOPT_SSL_VERIFYPEER=>0,
            CURLOPT_SSL_VERIFYHOST=>FALSE,
            CURLOPT_TIMEOUT=>config('flow')['timeout'],
            CURLOPT_COOKIEFILE=>$cookie_jar,
        );
        $dpi = $this->getDpi();
        $url = $dpi['url'].config('flow')['url'];
        Log::record('发送dpi地址：'.$url,'API');
        Log::record('发送dpi参数：'.$data_string,'API');
        $data = curl_post($url, $data_string, $curlopt);
        Log::record('dpi接口返回：'.$data,'API');
        $data = iconv('gb2312', 'utf-8', $data);

        if(strpos($data, config('panabit')['content_error'] ) !== false ){
            //重新登录，重新获取数据
            $cookie_jar = $this->getPanabitCookieJar(1);
            Log::record('发送dpi地址：'.$url,'API');
            Log::record('发送dpi参数：'.$data_string,'API');
            $data = curl_post($url, $data_string, $curlopt);
            Log::record('dpi接口返回：'.$data,'API');
            $data = iconv('gb2312', 'utf-8', $data);
             if(strpos($data, config('panabit')['content_error'] ) !== false ){
                $this->error(__('Data Access failed, please try again later'));
             }
        }

        //格式化json
        if($type == 'trenddown' || $type == 'trendup' || $type == 'snap'){
            $data = str_replace("'", '"', $data);
            $data_arr = json_decode($data,true);
        }else if($type == 'flow' || $type == 'link'){
            $json_arr = explode(',', $data);
            $names = array();
            $datas = array();
            $colors = array();
            //去掉最后一个，最后一个是空的
            for($i=0; $i<count($json_arr)-1; $i++){
                $temp = explode(' ', $json_arr[$i]);
                $names[] = $temp[0];
                $datas[] = $temp[1];
                $colors[] = $temp[2];
            } 
            $data_arr = array(
                'names'=>$names,
                'datas'=>$datas,
                'colors'=>$colors
            );
        }
        $this->success('', $data_arr);
    }

}
